//
//  BIDDetailEmailFromDraftboxViewController.m
//  shangwuting
//
//  Created by mal on 13-12-27.
//  Copyright (c) 2013年 mal. All rights reserved.
//

#import "BIDDetailEmailFromDraftboxViewController.h"

//详情
static NSString *strDetailURL = @"emailAndroid!toSendDraftEmail.action";
//重发
static NSString *strResendURL = @"emailAndroid!sendDraftEmail.action";

@interface BIDDetailEmailFromDraftboxViewController ()

@end

@implementation BIDDetailEmailFromDraftboxViewController
@synthesize titleLabel;
@synthesize timeLabel;
@synthesize receiverLabel;
@synthesize contentLabel;
@synthesize myTableView;
@synthesize docsArr;
@synthesize emailId;
@synthesize bgImgView1;
@synthesize myScrollView;
@synthesize lineLabel;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"邮件详情";
    // Do any additional setup after loading the view from its nib.
    [contentLabel setBackgroundColor:[UIColor clearColor]];
    [contentLabel setFont:[UIFont systemFontOfSize:[AppDelegate getFontSize]]];
    contentLabel.numberOfLines = 0;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"重发" style:UIBarButtonItemStyleBordered target:self action:@selector(resendBtnHandler)];
    
    //添加背景
    if([UIScreen mainScreen].bounds.size.height>=568)
    {
    }
    else
    {
    }
    
    [self initLayout];
    
    bgImgView1.frame = myTableView.frame;
    myTableView.backgroundView = bgImgView1;
    [self loadContent];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    titleLabel = nil;
    timeLabel = nil;
    receiverLabel = nil;
    contentLabel = nil;
    myTableView = nil;
    bgImgView1 = nil;
}

- (void)initLayout
{
    CGSize screenSize = [UIScreen mainScreen].bounds.size;
    CGRect ownFrame = self.view.frame;
    ownFrame.size.height = screenSize.height - 64;
    self.view.frame = ownFrame;
    myScrollView.frame = ownFrame;
}

- (void)loadContent
{
    NSString *strRequestURL = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getServerAddr], strDetailURL];
    NSString *strPost = [[NSString alloc] initWithFormat:@"id=%@", emailId];
    [self sendHttpRequestWithURL:strRequestURL postData:strPost withCompletion:^(NSDictionary *responseDic, NSString *errMsg){
        if(responseDic)
        {
            NSDictionary *emailDictionary = responseDic;
            titleLabel.text = [emailDictionary objectForKey:@"title"];
            timeLabel.text = [[NSString alloc] initWithFormat:@"时间:%@",[emailDictionary objectForKey:@"time"]];
            receiverLabel.text = [[NSString alloc] initWithFormat:@"接收人:%@",[emailDictionary objectForKey:@"receiveNames"]];
            contentLabel.text = [emailDictionary objectForKey:@"content"];
            //
            CGSize contentLabelSize = [contentLabel.text sizeWithFont:contentLabel.font constrainedToSize:CGSizeMake(contentLabel.frame.size.width, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping];
            CGFloat contentHeight = contentLabelSize.height>82?contentLabelSize.height+1:82;
            CGRect contentLabelFrame = contentLabel.frame;
            contentLabelFrame.size.height = contentHeight;
            contentLabel.frame = contentLabelFrame;
            //
            CGRect lineLabelFrame = lineLabel.frame;
            lineLabelFrame.origin.y = contentLabel.frame.origin.y + contentLabel.frame.size.height;
            lineLabel.frame = lineLabelFrame;
            //
            CGRect myTableViewFrame = myTableView.frame;
            myTableViewFrame.origin.y = lineLabel.frame.origin.y + lineLabel.frame.size.height + 3;
            myTableView.frame = myTableViewFrame;
            //
            CGSize myScrollViewContentSize = CGSizeMake(myScrollView.frame.size.width, myTableView.frame.origin.y+myTableView.frame.size.height+3);
            [myScrollView setContentSize:myScrollViewContentSize];
            //
            docsArr = [responseDic objectForKey:@"others"];
            if((ISNULL(docsArr)) || docsArr.count==0)
            {
                myTableView.hidden = YES;
            }
            else
            {
                [myTableView reloadData];
            }
        }
    }];
}

//重发
- (void)resendBtnHandler
{
    NSString *strTitle = titleLabel.text;
    strTitle = strTitle.length>0?strTitle:@"";
    NSString *strContent = contentLabel.text;
    strContent = strContent.length>0?strContent:@"";
    NSString *strRequestURL = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getServerAddr], strResendURL];
    NSString *strPost = [[NSString alloc] initWithFormat:@"id=%@&title=%@&content=%@", emailId, strTitle, strContent];
    [self sendHttpRequestWithURL:strRequestURL postData:strPost withCompletion:^(NSDictionary *responseDic, NSString *errMsg){
        if(responseDic)
        {
            NSNumber *value = [responseDic objectForKey:@"isSuccess"];
            if([value intValue]==1)
            {
                [BIDGlobalMethods showAlertView:@"发送成功" buttonTitle:@"关闭" delegate:self tag:0];
            }
            else
            {
                [BIDGlobalMethods showAlertView:@"发送失败" buttonTitle:@"关闭" delegate:nil tag:0];
            }
        }
    }];
}

#pragma mark UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [docsArr count];
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"identifier";
    NSUInteger row = [indexPath row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if(!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    NSDictionary *dictionary = [docsArr objectAtIndex:row];
    NSString *strFileName = [dictionary objectForKey:@"fileName"];
    NSString *strFileSize = [dictionary objectForKey:@"size"];
    strFileSize = [[NSString alloc] initWithFormat:@"(%@)", strFileSize];
    NSUInteger len1 = strFileName.length;
    NSUInteger len2 = strFileSize.length;
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:[[NSString alloc] initWithFormat:@"%@%@", strFileName, strFileSize]];
    [str addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0, len1)];
    [str addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:47.0f/255.0f green:132.0f/255.0f blue:61.0f/255.0f alpha:1.0f] range:NSMakeRange(len1, len2)];
    [str addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:NSUnderlineStyleSingle] range:NSMakeRange(0, len1)];
    cell.textLabel.attributedText = str;
    cell.textLabel.font = [UIFont systemFontOfSize:13.0f];
    [cell setBackgroundColor:[UIColor clearColor]];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 22;
}

#pragma mark UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    [self.navigationController popViewControllerAnimated:YES];
}

@end
